// Second example of jQuery and key input handling
// this js will handle multi-key input.

// Game running structure:

//  loopHead:
//      get input
//      process input
//      update internals
//      check for game over
//      goto loopHead
//
//  we loop around after a given interval.. eg. frame rate of 30 per second
//

//NB.   paddleA is the LHS one
//      paddleB is the RHS
//
var KEY = {
        UP: 38,
        DOWN: 40,
        W: 87,
        S: 83
    },
    pingpong = {pressedKeys:[]};
        
$(function(){
    // set interval to call gameloop every 30 milliseconds
    pingpong.timer = setInterval(gameloop,30);
    
    // mark down what key is down and up into an array called "pressedKeys"
    $(document).keydown(function(e){
        pingpong.pressedKeys[e.which] = true;
    });
    $(document).keyup(function(e){
        pingpong.pressedKeys[e.which] = false;
    });
});

// the loop function
function gameloop() {
    movePaddles();
}


function movePaddles() {
    // use our custom timer to continuously check if a key is pressed.
    if (pingpong.pressedKeys[KEY.UP]) { // arrow-up
        // move the paddle B up 5 pixels
        var top = parseInt($("#paddleB").css("top"));
        $("#paddleB").css("top",top-5);
    }
    if (pingpong.pressedKeys[KEY.DOWN]) { // arrow-down
        // move the paddle B down 5 pixels
        var top = parseInt($("#paddleB").css("top"));
        $("#paddleB").css("top",top+5);
    }
    if (pingpong.pressedKeys[KEY.W]) { // w
        // move the paddle A up 5 pixels
        var top = parseInt($("#paddleA").css("top"));
        $("#paddleA").css("top",top-5);
    }
    if (pingpong.pressedKeys[KEY.S]) { // s
        // move the paddle A down 5 pixels
        var top = parseInt($("#paddleA").css("top"));
        $("#paddleA").css("top",top+5);
    }
}

